package com.java46.carera.staff;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.carera.base.entity.Department;
import com.java46.carera.base.entity.JsonResult;
import com.java46.carera.base.entity.Positi;
import com.java46.carera.base.entity.Staff;
import com.java46.carera.base.mapper.StaffMapper;
import com.java46.carera.base.util.JsonUtil;
import com.java46.carera.department.DepartmentService;
import com.java46.carera.position.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.List;

@Service
public class StaffService {
    public StaffService(){System.out.println("StaffService...");}

    @Autowired
    private StaffMapper staffMapper;

     // 添加
    public void addStaff(String staff_name, String staff_sex, Integer staff_department, Integer staff_position,
                         String staff_cell_phone_number, String staff_id_number){

        Staff staff = new Staff();
        staff.setStaname(staff_name);
        staff.setStasex(staff_sex);

        Department department = new Department();
        department.setDepid(staff_department);
        staff.setDepid(department);

        Positi positi = new Positi();
        positi.setPosid(staff_position);
        staff.setPosid(positi);

        staff.setStaphone(staff_cell_phone_number);
        staff.setStaidentity(staff_id_number);

        int addStaff = staffMapper.insert(staff);
        if(addStaff > 0){
            JOptionPane.showMessageDialog(null, "添加成功！");
        }
        else{ JOptionPane.showMessageDialog(null, "添加失败！");}
    }

    // 修改前的查询
    public JsonResult selectStaff(Integer staff_id){
        Staff staff = staffMapper.selectByPrimaryKey(staff_id);
        return JsonUtil.setJsonResult(true, "",staff);
    }

    // 修改
    public void uplStaff(String staff_name, String staff_sex, Integer staff_department, Integer staff_position,
                         String staff_cell_phone_number, String staff_id_number, Integer staff_id){

        Staff staff = new Staff();
        staff.setStaname(staff_name);
        staff.setStasex(staff_sex);

        Department department = new Department();
        department.setDepid(staff_department);
        staff.setDepid(department);

        Positi positi = new Positi();
        positi.setPosid(staff_position);
        staff.setPosid(positi);

        staff.setStaphone(staff_cell_phone_number);
        staff.setStaidentity(staff_id_number);

        staff.setStaid(staff_id);

        int uplstaff = staffMapper.updateByPrimaryKey(staff);
        if (uplstaff>0){
            JOptionPane.showMessageDialog(null, "修改成功！");
        }else{
            JOptionPane.showMessageDialog(null, "修改失败！");
        }
    }

    // 分页查询
    public JsonResult selectStaffAll(Integer pagenNum,String keyword){
        //获取第1页，5条内容，默认查询总数count
        PageHelper.startPage(pagenNum,4);
        List<Staff> selectStaff = staffMapper.selectAll(keyword);
        PageInfo<Staff> page = new PageInfo<>(selectStaff);
        return JsonUtil.setJsonResult(true, "",page);
    }

    // 查询所有
    public JsonResult selectAll(){

        List<Staff> selectStaff = staffMapper.selectAll("");
        return JsonUtil.setJsonResult(true, "",selectStaff);
    }

    // 删除
    public void delStaff(Integer staff_id){
        int delPosition = staffMapper.deleteByPrimaryKey(staff_id);
        if (delPosition > 0){
            JOptionPane.showMessageDialog(null, "删除成功！");
        }else{
            JOptionPane.showMessageDialog(null, "删除失败！");
        }
    }
}
